$(function() {
	init();
});

/*
 * 获取初始化数据
 */

var commonData;

function init() {
	var jsonParam = {};
	var json = sessionStorage.getItem("appBase");
//	console.log(json);
	json = JSON.parse(json);
	jsonParam.appId = json.appId;
	jsonParam.orderId = json.orderId;
	jsonParam.sign = json.sign;
	padMarketing.ajaxNormal('/pad_marketing/main/editMdp.action',jsonParam,
		function(data){
			console.log(data);
			commonData = data;
			displayData(data);
		},
		function(data){
			alert('失败');
		}
	);
}

/*
 * 显示数据
 */
function displayData(data) {
	if(data.mdpFrequency != "N") {
		if(data.mdpFrequency == "01") {
			$("#mqj1").attr("checked", "checked");
		} else if(data.mdpFrequency == "03") {
			showData(2, data);
		} else if(data.mdpFrequency == "08") {
			showData(4, data);
		} else if(data.mdpFrequency == "02") {
			showData(6, data);
		}
	} else {
		$(".radio_judge").children().eq(0).removeClass("radio_yes").addClass("radio_no");
		$(".radio_judge").children().eq(1).removeClass("radio_no").addClass("radio_yes");
		$("#mqj-box").css("display", "none");
	}
	show();
}

function showData(id, data) {
	if(id == 4) {
		$("#mdpInterval").val(data.mdpInterval);
	}
	$("#mqj" + id).attr("checked", "checked");
	$("#mdpTermTimes" + id).val(data.mdpTermTimes);
	if(data.mdpMethod == "01") {
		$("#mqj1-" + (id - 1)).attr("checked", "checked");
	} else {
		$("#mqj1-" + id).attr("checked", "checked");
	}
	$("#mdpAccelerate" + id).val(data.mdpAccelerate);
	if(data.mdpMethodInterest == "01") {
		$("#mqj1-7").attr("checked", "checked");
	} else {
		$("#mqj1-8").attr("checked", "checked");
	}
}

function show() {
	if (document.getElementById("mqj1").checked) {
		document.getElementById("div1").style.display = "none";
	} else {
		document.getElementById("div1").style.display = "block";
	}
	if (document.getElementById("mqj2").checked) {
		document.getElementById("div2").style.display = "block";
	} else {
		document.getElementById("div2").style.display = "none";
	}
	if (document.getElementById("mqj4").checked) {
		document.getElementById("div3").style.display = "block";
	} else {
		document.getElementById("div3").style.display = "none";
	}
	if (document.getElementById("mqj6").checked) {
		document.getElementById("div4").style.display = "block";
	} else {
		document.getElementById("div4").style.display = "none";
	}
}

function myCheck(action) {
	if(action == 1) {
		commonData.mdpFrequency = $("input[name='xx']:checked").val();
	} else if(action == 2) {
		commonData.mdpFrequency = "N";
	}
}

function nextStep() {
//	console.log(commonData.mdpFrequency);
	if(commonData.mdpFrequency != "N") {
		commonData.mdpFrequency = $("input[name='xx']:checked").val();
		commonData.mdpMethodInterest = $("input[name='mqj1-4']:checked").val();
		if(commonData.mdpFrequency == "01") {
			commonData.mdpInterval = "";
			commonData.mdpTermTimes = "";
			commonData.mdpAccelerate = "";
			commonData.mdpMethod = "";
			commonData.mdpMethodInterest = "";
		} else if(commonData.mdpFrequency == "03") {
			commonData.mdpInterval = "";
			if($("#mdpTermTimes2").val() == "") {
				alertMsg("请输入领取年数");
				return ;
			}
			if(!$("input[name='mqj1-1']:checked").val()) {
				alertMsg("请选择领取次数");
				return ;
			}
			
			var str = $("#mdpAccelerate2").val().toString().split('.')[1];
			
			if($("input[name='mqj1-1']:checked").val() == "02" && $("#mdpAccelerate2").val() == "") {
				alertMsg("请输入递增率");
				return ;
			} else if($("#mdpAccelerate2").val() > 10 || $("#mdpAccelerate2").val() < 1) {
				alertMsg("递增比例最小为1最大为10(1~10)");
				return ;
			} else if(str && str.length > 1) {
				alertMsg("递增比例精度为小数点后一位");
				return ;
			}
			commonData.mdpTermTimes = $("#mdpTermTimes2").val();
			commonData.mdpAccelerate = $("#mdpAccelerate2").val();
			commonData.mdpMethod = $("input[name='mqj1-1']:checked").val();
		} else if(commonData.mdpFrequency == "08") {
			if($("#mdpInterval").val() == "") {
				alertMsg("请输入领取间隔的年数");
				return ;
			}
			if($("#mdpTermTimes4").val() == "") {
				alertMsg("请输入领取次数");
				return ;
			}
			if(!$("input[name='mqj1-2']:checked").val()) {
				alertMsg("请选择领取方式");
				return ;
			}
			
			var str = $("#mdpAccelerate4").val().toString().split('.')[1];
			
			if($("input[name='mqj1-2']:checked").val() == "02" && $("#mdpAccelerate4").val() == "") {
				alertMsg("请输入递增率");
				return ;
			} else if($("#mdpAccelerate4").val() > 10 || $("#mdpAccelerate4").val() < 1) {
				alertMsg("递增比例最小为1最大为10(1~10)");
				return ;
			} else if(str && str.length > 1) {
				alertMsg("递增比例精度为小数点后一位");
				return ;
			}
			commonData.mdpInterval = $("#mdpInterval").val();
			commonData.mdpTermTimes = $("#mdpTermTimes4").val();
			commonData.mdpAccelerate = $("#mdpAccelerate4").val();
			commonData.mdpMethod = $("input[name='mqj1-2']:checked").val();
		} else if(commonData.mdpFrequency == "02") {
			commonData.mdpInterval = "";
			if($("#mdpTermTimes6").val() == "") {
				alertMsg("请输入领取次数");
				return ;
			}
			if(!$("input[name='mqj1-3']:checked").val()) {
				alertMsg("请选择领取方式");
				return ;
			}
			
			var str = $("#mdpAccelerate6").val().toString().split('.')[1];
			
			if($("input[name='mqj1-3']:checked").val() == "02" && $("#mdpAccelerate6").val() == "") {
				alertMsg("请输入递增率");
				return ;
			} else if($("#mdpAccelerate6").val() > 10 || $("#mdpAccelerate6").val() < 1) {
				alertMsg("递增比例最小为1最大为10(1~10)");
				return ;
			} else if(str && str.length > 1) {
				alertMsg("递增比例精度为小数点后一位");
				return ;
			}
			commonData.mdpTermTimes = $("#mdpTermTimes6").val();
			commonData.mdpAccelerate = $("#mdpAccelerate6").val();
			commonData.mdpMethod = $("input[name='mqj1-3']:checked").val();
		} else {
			alertMsg("请至少选择一种领取方式");
			return ;
		}
		if(commonData.mdpFrequency != "01" && !$("input[name='mqj1-4']:checked").val()) {
			alertMsg("请选择满期金累积生息领取方式");
			return ;
		}
	} else {
		commonData.mdpInterval = "";
		commonData.mdpTermTimes = "";
		commonData.mdpAccelerate = "";
		commonData.mdpMethod = "";
		commonData.mdpMethodInterest = "";
//		window.location.href = "/pad_marketing/index/index.action";
	}
	saveData();
}

function saveData() {
	var da = JSON.stringify(commonData);
	console.log(commonData);
	var param = {"jsonParam":da};
	padMarketing.ajaxNormal('/pad_marketing/main/saveMdp.action',commonData,
		function(data){
			window.location.href = "/pad_marketing/main/gotoHealthContent.action";
		},
		function(data){
			console.log(data);
			alertMsg(data.responseText);
		}
	);
	/*$.ajax({
		type : "post",
		url : "/pad_marketing/main/saveMdp.action",
		data : param,
		async : true,
		success : function(data) {
			data = eval( '(' + data + ')' );
			console.log(data.Status);
			if(data.Status == "S") {
				window.location.href = "/pad_marketing/main/gotoHealthContent.action";
			} else {
				alertMsg(data.ErrMsg);
			}
		},
		error : function(data) {
//			console.log(data);
			alertMsg(data.responseText);
		}
	});*/
}